package until;

import java.io.*;
import java.util.HashMap;

/**
 * @Description
 * @auther lileilei
 * @create 2020-03-16 15:23
 */
public class AppiumServerController {

    private HashMap<String, Process> processHashMap = new HashMap<>();
    public void startServer(String UID,String  port ) throws Exception {
        Process process;

        String cmd = "appium " + "--session-override " + " -p "
                + port+  " -U " + UID;
        process = Runtime.getRuntime().exec(cmd);
        processHashMap.put(String.valueOf(port), process);
        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        process.waitFor();
        inputStream.close();
        reader.close();
        process.destroy();
    }

     public void stopServer(Process process) {

        if (process != null) {
            System.out.println(process);
            process.destroy();
        }
    }

    public void stopServer(String port) {
        Process process = processHashMap.get(port);
        stopServer(process);
        processHashMap.remove(port);
    }

}
